package dto

import (
	"flare-admin/db/sqlc/ddos"
	"flare-admin/http/model"
	"flare-admin/http/request"
)

// DDoSFlowRequest2DTO 将请求转换为DTO
func DDoSFlowRequest2DTO(req *request.DDoSFlowRequest) ddos.InsertDDoSFlowParams {
	return ddos.InsertDDoSFlowParams{
		Ip:                          req.IP,
		Protocol:                    int32(req.Protocol),
		FlowDuration:                req.FlowDuration,
		FwdPackets:                  int32(req.FwdPackets),
		FlowType:                    req.FlowType,
		Time:                        req.Time,
		FeaturesProtocol:            int32(req.Features.Protocol),
		FeaturesFlowDuration:        req.Features.FlowDuration,
		FeaturesTotalFwdPackets:     int32(req.Features.TotalFwdPackets),
		FeaturesTotalBackwardPackets: int32(req.Features.TotalBackwardPackets),
		FeaturesFwdPacketsLengthTotal: req.Features.FwdPacketsLengthTotal,
		FeaturesFwdPacketLengthMax:  req.Features.FwdPacketLengthMax,
		FeaturesFwdPacketLengthMin:  req.Features.FwdPacketLengthMin,
		FeaturesFwdPacketLengthStd:  req.Features.FwdPacketLengthStd,
		FeaturesBwdPacketLengthMax:  req.Features.BwdPacketLengthMax,
		FeaturesBwdPacketLengthMin:  req.Features.BwdPacketLengthMin,
		FeaturesFlowIatMean:         req.Features.FlowIATMean,
		FeaturesFlowIatMin:          req.Features.FlowIATMin,
		FeaturesBwdIatMean:          req.Features.BwdIATMean,
		FeaturesBwdIatMin:           req.Features.BwdIATMin,
		FeaturesFwdPshFlags:         int32(req.Features.FwdPSHFlags),
		FeaturesFwdHeaderLength:     int32(req.Features.FwdHeaderLength),
		FeaturesBwdHeaderLength:     int32(req.Features.BwdHeaderLength),
		FeaturesBwdPacketsPerSecond: req.Features.BwdPacketsPerSecond,
		FeaturesSynFlagCount:        int32(req.Features.SYNFlagCount),
		FeaturesUrgFlagCount:        int32(req.Features.URGFlagCount),
		FeaturesCweFlagCount:        int32(req.Features.CWEFlagCount),
		FeaturesDownUpRatio:         req.Features.DownUpRatio,
		FeaturesInitFwdWinBytes:     int32(req.Features.InitFwdWinBytes),
		FeaturesInitBwdWinBytes:     int32(req.Features.InitBwdWinBytes),
		FeaturesActiveMean:          req.Features.ActiveMean,
		FeaturesActiveStd:           req.Features.ActiveStd,
		FeaturesIdleStd:             req.Features.IdleStd,
	}
}

// DDoSFlowDB2Model 将数据库模型转换为HTTP模型
func DDoSFlowDB2Model(dbFlow *ddos.OteldemoDdosFlow) model.DDoSFlow {
	return model.DDoSFlow{
		IP:           dbFlow.Ip,
		Protocol:     int(dbFlow.Protocol),
		FlowDuration: dbFlow.FlowDuration,
		FwdPackets:   int(dbFlow.FwdPackets),
		FlowType:     dbFlow.FlowType,
		Time:         dbFlow.Time,
		Features: model.Features{
			Protocol:                int(dbFlow.FeaturesProtocol),
			FlowDuration:           dbFlow.FeaturesFlowDuration,
			TotalFwdPackets:        int(dbFlow.FeaturesTotalFwdPackets),
			TotalBackwardPackets:   int(dbFlow.FeaturesTotalBackwardPackets),
			FwdPacketsLengthTotal:  dbFlow.FeaturesFwdPacketsLengthTotal,
			FwdPacketLengthMax:     dbFlow.FeaturesFwdPacketLengthMax,
			FwdPacketLengthMin:     dbFlow.FeaturesFwdPacketLengthMin,
			FwdPacketLengthStd:     dbFlow.FeaturesFwdPacketLengthStd,
			BwdPacketLengthMax:     dbFlow.FeaturesBwdPacketLengthMax,
			BwdPacketLengthMin:     dbFlow.FeaturesBwdPacketLengthMin,
			FlowIATMean:            dbFlow.FeaturesFlowIatMean,
			FlowIATMin:             dbFlow.FeaturesFlowIatMin,
			BwdIATMean:             dbFlow.FeaturesBwdIatMean,
			BwdIATMin:              dbFlow.FeaturesBwdIatMin,
			FwdPSHFlags:            int(dbFlow.FeaturesFwdPshFlags),
			FwdHeaderLength:        int(dbFlow.FeaturesFwdHeaderLength),
			BwdHeaderLength:        int(dbFlow.FeaturesBwdHeaderLength),
			BwdPacketsPerSecond:    dbFlow.FeaturesBwdPacketsPerSecond,
			SYNFlagCount:           int(dbFlow.FeaturesSynFlagCount),
			URGFlagCount:           int(dbFlow.FeaturesUrgFlagCount),
			CWEFlagCount:           int(dbFlow.FeaturesCweFlagCount),
			DownUpRatio:            dbFlow.FeaturesDownUpRatio,
			InitFwdWinBytes:        int(dbFlow.FeaturesInitFwdWinBytes),
			InitBwdWinBytes:        int(dbFlow.FeaturesInitBwdWinBytes),
			ActiveMean:             dbFlow.FeaturesActiveMean,
			ActiveStd:              dbFlow.FeaturesActiveStd,
			IdleStd:                dbFlow.FeaturesIdleStd,
		},
	}
}

// DDoSFlowsDB2Model 将数据库模型列表转换为HTTP模型列表
func DDoSFlowsDB2Model(dbFlows []ddos.OteldemoDdosFlow) []model.DDoSFlow {
	flows := make([]model.DDoSFlow, len(dbFlows))
	for i, dbFlow := range dbFlows {
		flows[i] = DDoSFlowDB2Model(&dbFlow)
	}
	return flows
}

// DDoSFlowModel2DTO 将HTTP模型转换为更新DTO
func DDoSFlowModel2DTO(flow *model.DDoSFlow, id int32) ddos.UpdateDDoSFlowParams {
	return ddos.UpdateDDoSFlowParams{
		ID:                           id,
		Ip:                          flow.IP,
		Protocol:                    int32(flow.Protocol),
		FlowDuration:                flow.FlowDuration,
		FwdPackets:                  int32(flow.FwdPackets),
		FlowType:                    flow.FlowType,
		Time:                        flow.Time,
		FeaturesProtocol:            int32(flow.Features.Protocol),
		FeaturesFlowDuration:        flow.Features.FlowDuration,
		FeaturesTotalFwdPackets:     int32(flow.Features.TotalFwdPackets),
		FeaturesTotalBackwardPackets: int32(flow.Features.TotalBackwardPackets),
		FeaturesFwdPacketsLengthTotal: flow.Features.FwdPacketsLengthTotal,
		FeaturesFwdPacketLengthMax:  flow.Features.FwdPacketLengthMax,
		FeaturesFwdPacketLengthMin:  flow.Features.FwdPacketLengthMin,
		FeaturesFwdPacketLengthStd:  flow.Features.FwdPacketLengthStd,
		FeaturesBwdPacketLengthMax:  flow.Features.BwdPacketLengthMax,
		FeaturesBwdPacketLengthMin:  flow.Features.BwdPacketLengthMin,
		FeaturesFlowIatMean:         flow.Features.FlowIATMean,
		FeaturesFlowIatMin:          flow.Features.FlowIATMin,
		FeaturesBwdIatMean:          flow.Features.BwdIATMean,
		FeaturesBwdIatMin:           flow.Features.BwdIATMin,
		FeaturesFwdPshFlags:         int32(flow.Features.FwdPSHFlags),
		FeaturesFwdHeaderLength:     int32(flow.Features.FwdHeaderLength),
		FeaturesBwdHeaderLength:     int32(flow.Features.BwdHeaderLength),
		FeaturesBwdPacketsPerSecond: flow.Features.BwdPacketsPerSecond,
		FeaturesSynFlagCount:        int32(flow.Features.SYNFlagCount),
		FeaturesUrgFlagCount:        int32(flow.Features.URGFlagCount),
		FeaturesCweFlagCount:        int32(flow.Features.CWEFlagCount),
		FeaturesDownUpRatio:         flow.Features.DownUpRatio,
		FeaturesInitFwdWinBytes:     int32(flow.Features.InitFwdWinBytes),
		FeaturesInitBwdWinBytes:     int32(flow.Features.InitBwdWinBytes),
		FeaturesActiveMean:          flow.Features.ActiveMean,
		FeaturesActiveStd:           flow.Features.ActiveStd,
		FeaturesIdleStd:             flow.Features.IdleStd,
	}
}

